/**
 * apiRoute.js
 * API结构路由
 *
 * created by qiuwei
 * version 1.0.0
 * 2016年03月21日
 */

import express from 'express';
import * as CONSTANTS from '../../constants/API';
import { REPLY_DATA } from '../../constants/replyData';
import { HTTP_RESULT } from '../../constants/protocol';
import authenticate from './authenticate';
import { modifyPassword } from './authenticate';
import * as AREA from './area';
import subject from './subject';
import grade from './grade';
import * as BASIC from './basicData';
import * as ACTIVE from './activeData';
import * as JOURNAL from './teachingJournalData';
import * as MANAGEMENT from './management';
import * as KNOWLEDGE from './knowledge';
import moment from 'moment';

// 创建路由节点
const apiRoute = new express.Router();

/**
 * 错误处理
 * @param  {string} error 错误信息
 * @return {object}       约定的格式对象
 */
function handleError(error) {
	let errorText = error ? error.toString() : '未知错误';

	console.log(`出现错误: ${errorText},出现时间:${moment().format()}`);

	return Object.assign({}, REPLY_DATA, {
		code: HTTP_RESULT.FAILURE,
		data: {
			message: errorText
		}
	});
}

/**
 * 开启或关闭调试模式
 */
apiRoute.get('/debug', (req, res) => {
	global.DEBUG = !global.DEBUG;
	res.end(`DEBUG: ${global.DEBUG}`);
})

/**
 * 鉴权登录
 */
apiRoute.post('/authenticate', (req, res) => {
	authenticate(req.body)
	.then((result) => {
		res.json(result);
	})
	.catch((error) => res.json(handleError(error)));
});

/**
 * 修改密码
 */
apiRoute.post('/modify_password', (req, res) => {
	modifyPassword(req.body)
	.then((result) => {
		res.json(result);
	})
	.catch((error) => {
		res.json(handleError(error));
	});
});

/**
 * 获取区县
 */
apiRoute.get('/area/county/:city', (req, res) => {
	AREA.county(req.params.city)
		.then((result) => {
			res.json(result);
		});
});

/**
 * 获取学校
 */
apiRoute.get('/area/school/:county', (req, res) => {
	AREA.school(req.params.county)
		.then((result) => {
			res.json(result);
		})
});

/**
 * 获取科目
 */
apiRoute.get('/subject', (req, res) => {
	subject()
		.then((result) => {
			res.json(result);
		})
});

/**
 * 获取年级
 */
apiRoute.get('/grade', (req, res) => {
	grade()
		.then((result) => {
			res.json(result);
		});
});

/**
 * 获取基础概况基础表格数据(数字化教育概况)
 */
apiRoute.post('/basic/list/:area', (req, res) => {
	let promise = null;

	if (req.params.area == 'city') {
		promise = BASIC.cityCount(req.body);
	} else if (req.params.area == 'county') {
		promise = BASIC.countyCount(req.body);
	} else if (req.params.area == 'school') {
		promise = BASIC.schoolCount(req.body);
	} else {
		return res.json(handleError(`未定义的区域标识: ${req.params.area}`));
	}

	promise.then((result) => res.json(result))
			.catch((error) => res.json(handleError(error)));
});

/**
 * 获取基础概况图表数据
 */
apiRoute.post('/basic/chart/:area', (req, res) => {
	let promise = null;

	if (req.params.area == 'city') {
		promise = BASIC.cityChart(req.body);
	} else if (req.params.area == 'county') {
		promise = BASIC.countyChart(req.body);
	} else if (req.params.area == 'school') {
		promise = BASIC.schoolChart(req.body);
	} else {
		return res.json(handleError(`未定义的区域标识: ${req.params.area}`));
	}

	promise.then((result) => res.json(result))
			.catch((error) => res.json(handleError(error)));
});

/**
 * 获取基础概况数据详情
 */
apiRoute.post('/basic/detail/:area', (req, res) => {
	let promise = null;

	if (req.params.area == 'city') {
		promise = BASIC.cityDetail(req.body);
	} else if (req.params.area == 'county') {
		promise = BASIC.countyDetail(req.body);
	} else {
		return res.json(handleError(`未定义的区域标识: ${req.params.area}`));
	}

	promise.then((result) => res.json(result))
			.catch((error) => res.json(handleError(error)));
});

/**
 * 获取电子教学日志
 */
apiRoute.post('/teachingJournal', (req, res) => {
	let promise = JOURNAL.journal(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取活跃热力图基础数据
 */
apiRoute.post('/active/list/:area', (req, res) => {
	let promise = null;

	if (req.params.area == 'city') {
		promise = ACTIVE.cityCount(req.body);
	} else if (req.params.area == 'county') {
		promise = ACTIVE.countyCount(req.body);
	} else if (req.params.area == 'school') {
		promise = ACTIVE.schoolCount(req.body);
	} else {
		return res.json(handleError(`未定义的区域标识: ${req.params.area}`));
	}

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

apiRoute.post('/active/chart/:area', (req, res) => {
	let promise = null;

	if (req.params.area == 'city') {
		promise = ACTIVE.cityChart(req.body);
	} else if (req.params.area == 'county') {
		promise = ACTIVE.countyChart(req.body);
	} else {
		return res.json(handleError(`未定义的区域标识: ${req.params.area}`));
	}

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

apiRoute.post('/active/detail/:area', (req, res) => {
	let promise = null;
	
	if(req.params.area === 'city') {
		promise = ACTIVE.cityDetail(req.body);
	} else if(req.params.area === 'county') {
		promise = ACTIVE.countyDetail(req.body);
	} else {
		return res.json(handleError(`未定义的区域标识：${req.params.area}`));
	}

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取作业管理基础数据
 */
apiRoute.post('/management/list/:area', (req, res) => {
	let promise = null;

	if(req.params.area === 'city')
		promise = MANAGEMENT.viewCity(req.body);
	else if(req.params.area === 'county')
		promise = MANAGEMENT.viewCounty(req.body);
	else if(req.params.area === 'school')
		promise = MANAGEMENT.viewSchool(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取作业管理教师周布置频次
 */
apiRoute.post('/management/frequency/:area', (req, res) => {
	let promise = null;

	if(req.params.area === 'city')
		promise = MANAGEMENT.getCityFrequency(req.body);
	else if(req.params.area === 'county')
		promise = MANAGEMENT.getCountyFrequency(req.body);
	else if(req.params.area === 'school')
		promise = MANAGEMENT.getSchoolFrequency(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取作业管理平均作业量
 */
apiRoute.post('/management/average/:area', (req, res) => {
	let promise = null;

	if(req.params.area === 'city')
		promise = MANAGEMENT.getCityAverage(req.body);
	else if(req.params.area === 'county')
		promise = MANAGEMENT.getCountyAverage(req.body);
	else if(req.params.area === 'school')
		promise = MANAGEMENT.getSchoolAverage(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取作业管理题目难度分布
 */
apiRoute.post('/management/difficulty/:area', (req, res) => {
	let promise = null;

	if(req.params.area === 'city')
		promise = MANAGEMENT.getCityDifficulty(req.body);
	else if(req.params.area === 'county')
		promise = MANAGEMENT.getCountyDifficulty(req.body);
	else if(req.params.area === 'school')
		promise = MANAGEMENT.getSchoolDifficulty(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取作业管理题目类型分布
 */
apiRoute.post('/management/question_type/:area', (req, res) => {
	let promise = null;

	if(req.params.area === 'city')
		promise = MANAGEMENT.getCityQuestionType(req.body);
	else if(req.params.area === 'county')
		promise = MANAGEMENT.getCountyQuestionType(req.body);
	else if(req.params.area === 'school')
		promise = MANAGEMENT.getSchoolQuestionType(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取作业管理作业布置周期分布
 */
apiRoute.post('/management/period/:area', (req, res) => {
	let promise = null;

	if(req.params.area === 'city')
		promise = MANAGEMENT.getCityPeriod(req.body);
	else if(req.params.area === 'county')
		promise = MANAGEMENT.getCountyPeriod(req.body);
	else if(req.params.area === 'school')
		promise = MANAGEMENT.getSchoolPeriod(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取作业管理作业布置时段分布
 */
apiRoute.post('/management/timeround/:area', (req, res) => {
	let promise = null;

	if(req.params.area === 'city')
		promise = MANAGEMENT.getCityTimeround(req.body);
	else if(req.params.area === 'county')
		promise = MANAGEMENT.getCountyTimeround(req.body);
	else if(req.params.area === 'school')
		promise = MANAGEMENT.getSchoolTimeround(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取布置作业区县数据详情
 */
apiRoute.post('/management/detail/:area', (req, res) => {
	let promise = null;

	if(req.params.area === 'city') 
		promise = MANAGEMENT.getCityDetail(req.body);
	else if(req.params.area === 'county')
		promise = MANAGEMENT.getCountyDetail(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

/**
 * 获取知识点分布数据
 */
apiRoute.post('/knowledge/list/:area', (req, res) => {
	let promise = null;

	if(req.params.area === 'city')
		promise = KNOWLEDGE.viewCityList(req.body);
	else if(req.params.area === 'county')
		promise = KNOWLEDGE.viewCountyList(req.body);
	else if(req.params.area === 'school')
		promise = KNOWLEDGE.viewSchoolList(req.body);

	promise.then((result) => res.json(result))
		.catch((error) => res.json(handleError(error)));
});

export default apiRoute;